/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.CRM.bean.SalesLeadModuleRemote;
import Merlion.util.state.ClientState;
import Merlion.util.state.ContactState;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean(name = "ContactManagedBean")
@ViewScoped
public class ContactManagedBean implements Serializable {

    private ContactState newContact = new ContactState();
    private List<ContactState> contacts;//return list of contacts
    private ContactState[] selectedContacts;
    private ContactState selectedContact;
    private ContactState selectedViewContact = new ContactState();
    private List<ClientState> customerList;
    private boolean editMode;
    private boolean duplicate;
    private String message;
    @EJB
    private SalesLeadModuleRemote smb;

    public ContactManagedBean() {
      
    }

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (contacts == null) {
            contacts = smb.viewContacts();
        }
    }

    public boolean isDuplicate() {
        String dupString = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("duplicate");
        if (dupString.equals("true")) {
            duplicate = true;
        } else if (dupString.equals("false")) {
            duplicate = false;
        }

        return duplicate;
    }

    public void setDuplicate(boolean duplicate) {
        this.duplicate = duplicate;
    }

    public ContactState getNewContact() {
        return newContact;
    }

    public void setNewContact(ContactState newContact) {
        this.newContact = newContact;
    }

    public List<ContactState> getContacts() {
        return contacts;
    }

    public void setContacts(List<ContactState> contacts) {
        this.contacts = contacts;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public void addContact(ActionEvent event) {
        System.out.println(":::WEB::: Add new contact");
        Long returnId = Long.parseLong("0");
        if (newContact != null) {
            String salutation = newContact.getSalutation();
            String firstName = newContact.getFirstName();
            String lastName = newContact.getLastName();
            String mobileNum = newContact.getMobileNum();
            String faxNum = newContact.getFaxNum();
            String address = newContact.getAddress();
            String contactNum = newContact.getContactNum();
            String country = newContact.getCountry();
            String city = newContact.getCity();
            String postalCode = newContact.getPostalCode();
            String emailAddress = newContact.getEmailAddress();
            String leadSource = newContact.getLeadSource();
            String description = newContact.getDescription();
            String title = newContact.getTitle();
            String department = newContact.getDepartment();
            Date dob = newContact.getDob();
            String addressState = newContact.getAddressState();
            Long companyId = newContact.getCompanyId();
            String companyName = newContact.getCompanyName();
       

            returnId = smb.addContact(salutation, firstName, lastName, companyId, companyName,
                    leadSource, title, department, contactNum, mobileNum, faxNum,
                    emailAddress, address, addressState, country, city, postalCode,
                    dob, description);
        }

        if (returnId != 0) {
            String successMessage = "You have sucessfully added a new contact!";
            setDuplicate(false);
            setMessage(successMessage);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("duplicate", "false");
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("message", successMessage);
        } else {
            setDuplicate(true);
            String failMessage = "Duplicate contact data found in the database!" + "\n"
                    + "\t\t\tContact is not added to the system.";
            setMessage(failMessage);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("duplicate", "true");
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("message", failMessage);
        }

        System.out.println(":::WEB::: Contact ID is : " + returnId);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("createNewContactStatus.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public String getMessage() {
        if (message == null) {
            message = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("message");
        }
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void onEditRow(RowEditEvent event) {
        System.out.println(":::WEB::: On Edit Row");
        ContactState eContact = (ContactState) event.getObject();

        if (eContact != null) {
            System.out.println(":::WEB::: Edited Contact not null");
            String eSalutation = eContact.getSalutation();
            String eFirstName = eContact.getFirstName();
            String eLastName = eContact.getLastName();
            String eMobileNum = eContact.getMobileNum();
            String eFaxNum = eContact.getFaxNum();
            String eAddress = eContact.getAddress();
            String eContactNum = eContact.getContactNum();
            String eCountry = eContact.getCountry();
            String eCity = eContact.getCity();
            String ePostalCode = eContact.getPostalCode();
            String eEmailAddress = eContact.getEmailAddress();
            String eLeadSource = eContact.getLeadSource();
            String eDescription = eContact.getDescription();
            String eTitle = eContact.getTitle();
            String eDepartment = eContact.getDepartment();
            Date eDob = eContact.getDob();
            String eAddressState = eContact.getAddressState();
            Long eCompanyId = eContact.getCompanyId();
            String eCompanyName = eContact.getCompanyName();
            Long contactId = eContact.getId();

            System.out.println(":::WEB::: Calling Edit contact");
            smb.editContact(contactId, eSalutation, eFirstName, eLastName, eCompanyId, eCompanyName,
                    eLeadSource, eTitle, eDepartment, eContactNum, eMobileNum, eFaxNum, eEmailAddress,
                    eAddress, eAddressState, eCountry, eCity, ePostalCode, eDob, eDescription);
        }
    }

    public void save(ActionEvent event) {
        System.out.println(":::WEB::: Save Edit Contact");

        Long contactId = selectedViewContact.getId();
        String cSalutation = selectedViewContact.getSalutation();
        String cFirstName = selectedViewContact.getFirstName();
        String cLastName = selectedViewContact.getLastName();
        String cMobileNum = selectedViewContact.getMobileNum();
        String cFaxNum = selectedViewContact.getFaxNum();
        String cContactNumber = selectedViewContact.getContactNum();
        String cAddress = selectedViewContact.getAddress();
        String cCountry = selectedViewContact.getCountry();
        String cCity = selectedViewContact.getCity();
        String cPostalCode = selectedViewContact.getPostalCode();
        String cEmailAddress = selectedViewContact.getEmailAddress();
        String cLeadSource = selectedViewContact.getLeadSource();
        String cDescription = selectedViewContact.getDescription();
        String cAddressState = selectedViewContact.getAddressState();
        String cTitle = selectedViewContact.getTitle();
        String cDepartment = selectedViewContact.getDepartment();
        Date cDob = selectedViewContact.getDob();
        Long cCompanyId = selectedViewContact.getCompanyId();
        String cCompanyName = selectedViewContact.getCompanyName();

        System.out.println("Contact ID is :" + contactId);
        System.out.println(":::WEB::: Calling edit Contact");
        smb.editContact(contactId, cSalutation, cFirstName, cLastName,
                cCompanyId, cCompanyName, cLeadSource, cTitle, cDepartment,
                cContactNumber, cMobileNum, cFaxNum, cEmailAddress, cAddress,
                cAddressState, cCountry, cCity, cPostalCode, cDob, cDescription);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("contacts.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void delete(ActionEvent Event) {

        System.out.println(":::WEB::: Delete Contact");

        if (selectedContact != null) {
            System.out.println(":::WEB::: Selected contact not null");
            Long contactId = selectedContact.getId();
            System.out.println(":::WEB::: Contact ID is : " + contactId);
            smb.deleteContact(contactId);
            System.out.println(":::WEB::: Deletion done");
        } else {
            System.out.println(":::WEB::: Selected contact is null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("contacts.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void deleteContacts() {
        System.out.println(":::WEB::: Delete Contacts");

        if (selectedContacts != null) {
            System.out.println(":::WEB::: If selected Contacts not null");
            for (int i = 0; i < selectedContacts.length; i++) {
                System.out.println("selectedContacts i: " + i + " - " + selectedContacts[i].getId());
                smb.deleteContact(selectedContacts[i].getId());
            }
        } else {
            System.out.println(":::WEB::: Selected Contacts are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("contacts.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void cancelDelete() {
        for (ContactState contact : selectedContacts) {
            contact = null;
        }
        if (selectedContact == null) {
            System.out.println(":::WEB::: Deletion of Contacts cancelled");

        } else {
            System.out.println(":::WEB::: Deletion of Contacts are not cancelled");
        }

    }

    public List<ClientState> getCustomerList() {
        if (customerList == null) {
            customerList = smb.viewCustomers();
        }
        return customerList;
    }

    public void setCustomerList(List<ClientState> customerList) {
        this.customerList = customerList;
    }

  
    public ContactState[] getSelectedContacts() {
        return selectedContacts;
    }

    public void setSelectedContacts(ContactState[] selectedContacts) {
        this.selectedContacts = selectedContacts;
    }

    public ContactState getSelectedContact() {
        return selectedContact;
    }

    public void setSelectedContact(ContactState selectedContact) {
        this.selectedContact = selectedContact;
    }

    public ContactState getSelectedViewContact() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (selectedViewContact == null) {
            selectedViewContact = new ContactState();
        }
        return selectedViewContact;
    }

    public void setSelectedViewContact(ContactState selectedViewContact) {
        System.out.println("SET Selected view contact");
        this.selectedViewContact = selectedViewContact;
    }

}
